package com.txf.coomon.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.google.gson.JsonElement;
import com.txf.coomon.R;
import com.txf.coomon.ben.DemoItem;
import com.txf.coomon.mvp.net_okhttp.contract.DemoContract;
import com.txf.coomon.mvp.net_okhttp.presenter.DemoPresenter;
import com.txf.ui_mvplibrary.interfaces.OnAppListener;
import com.txf.ui_mvplibrary.ui.adapter.BaseCompleteRecyclerAdapter;
import com.txf.ui_mvplibrary.ui.adapter.BaseRecyclerAdapter;
import com.txf.ui_mvplibrary.ui.adapter.hoder.BaseViewHoder;
import com.txf.ui_mvplibrary.ui.fragment.BaseMVPRecyclerViewFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @author txf
 * @create 2019/2/13 0013
 * @
 */

public class OkHttpFragment extends BaseMVPRecyclerViewFragment<DemoItem, DemoContract.View, DemoContract.Presenter>
        implements DemoContract.View, OnAppListener.OnAdapterListener {

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        setContract(this, new DemoPresenter(getContext()));
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRefreshLayout.setBackgroundColor(0xff2577e3);
    }

    @Override
    public void httpOnSuccess(int tag, JsonElement data, String message) {

    }

    @Override
    public void httpOnError(int tag, int error, String message) {

    }

    @Override
    protected BaseRecyclerAdapter<DemoItem> buildAdapter() {
        return new BaseCompleteRecyclerAdapter<DemoItem>(getContext(), this) {
            @Override
            protected void setItemLayout() {
                super.setItemLayout();
                addItemLayout(0, R.layout.item_view_simple);
            }

            @Override
            protected void onBindBaseViewHoder(BaseViewHoder holder, int position, final DemoItem item) {
                String value = (String) item.getData();
                holder.setText(R.id.value, value);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getListener().onInteractionAdapter(item.getAction(), null);
                    }
                });
            }
        };
    }

    @Override
    protected void request(int pageIndex, int pageSize) {
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                testItemData();
            }
        }, 1000);
    }
    private void testItemData() {
        List<DemoItem> items = new ArrayList<>();
        items.add(new DemoItem("测试", 0));
        items.add(new DemoItem("测试", 0));
        items.add(new DemoItem("测试", 0));
        items.add(new DemoItem("测试", 0));
        refreshData(items);
    }
    @Override
    public void onInteractionAdapter(int action, Bundle bundle) {

    }
}



